package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.PageInfo;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.PmsBrand;
import com.genesis.mall.mapper.PmsBrandMapper;
import com.genesis.mall.service.IPmsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements IPmsBrandService {


    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo listBrand(String keyword, Integer pageNum, Integer pageSize) {
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsBrand> wrapper = new LambdaQueryWrapper<>();
             wrapper.like(StringUtils.isNotEmpty(keyword), PmsBrand::getName, keyword).
              or().like(StringUtils.isNotEmpty(keyword), PmsBrand::getFirstLetter, keyword);
//             wrapper.eq(PmsBrand::getDeleteStatus,0);

         wrapper.orderByDesc(PmsBrand::getSort);

        this.page(page, wrapper);
        int total = (int) page.getTotal();
        int current = (int) page.getCurrent();

        //封装分页数据
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(total);
        pageInfo.setTotalPage(current);
        pageInfo.setList(page.getRecords());

        return pageInfo;
    }

    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     */
    @Override
    public void updateShowStatus(List<Long> ids, Integer showStatus) {
        List<PmsBrand> list = this.listByIds(ids);
        for (PmsBrand pmsBrand : list) {
            pmsBrand.setShowStatus(showStatus);
        }
        this.updateBatchById(list);
    }

    @Override
    public void updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        List<PmsBrand> list = this.listByIds(ids);
        for (PmsBrand pmsBrand : list) {
            pmsBrand.setFactoryStatus(factoryStatus);
        }
        this.updateBatchById(list);
    }
    @Override
    public List<PmsBrand> pageList(Integer pageNum, Integer pageSize) {

        Page<PmsBrand> pmsBrandPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsBrand> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsBrand::getShowStatus,1);
        lqw.orderByDesc(PmsBrand::getSort);
        //分页查询
        page(pmsBrandPage,lqw);


        return pmsBrandPage.getRecords();
    }
}
